﻿/*
 * My Timer - frmTimers.cs
 * 
 * Copyright (C) 2009 Ahmad Hanis Azmi
 * 
 * This file is part of My Timer
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using My_Timer.Formatting;

namespace My_Timer
{
    public partial class frmTimers : Form
    {
        public frmTimers()
        {
            InitializeComponent();

            this.numericUpDownDays.GotFocus += new EventHandler(numericUpDownDays_GotFocus);
            this.numericUpDownDays.Click += new EventHandler(numericUpDownDays_Click);

            this.numericUpDownHours.GotFocus += new EventHandler(numericUpDownHours_GotFocus);
            this.numericUpDownHours.Click += new EventHandler(numericUpDownHours_Click);

            this.numericUpDownMinutes.GotFocus += new EventHandler(numericUpDownMinutes_GotFocus);
            this.numericUpDownMinutes.Click += new EventHandler(numericUpDownMinutes_Click);

            this.numericUpDownSeconds.GotFocus += new EventHandler(numericUpDownSeconds_GotFocus);
            this.numericUpDownSeconds.Click += new EventHandler(numericUpDownSeconds_Click);

            this.buttonBrowseAudio.TextChanged +=new EventHandler(buttonBrowseAudio_TextChanged);
        }

        public static class AudioPath
        {
            public static string Path = "";
        }

        private string appLocation = "";

        private void buttonBrowseAudio_TextChanged(object sender, EventArgs e)
        {
            if (buttonBrowseAudio.Text.Length == 0) { buttonBrowseAudio.Text = "Browse..."; }
        }

        private void checkBoxOnTimerEndShowMsgbox_CheckedChanged(object sender, EventArgs e)
        {
            //System.Windows.Forms.MessageBox.Show(this.checkBoxOnTimerEndShowMsgbox.Checked.ToString());

            if (this.checkBoxOnTimerEndShowMsgbox.Checked == true)
            {
                this.textBoxOnTimerEndMsgbox.ReadOnly = false;
            }
            else
            {
                this.textBoxOnTimerEndMsgbox.ReadOnly = true;
            }
        }

        private void frmTimers_Load(object sender, EventArgs e)
        {
            if (GlobalClass.frmTimersStatus == "Add")
            {
                this.comboBoxOnTimerEnd.SelectedIndex = 0;
            }
        }

        private void frmTimers_FormClosed(object sender, EventArgs e)
        {
            frmTimers.AudioPath.Path = "";
            appLocation = "";
        }

        private void checkBoxOnTimerEndPlayAudio_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBoxOnTimerEndPlayAudio.Checked == true)
            {
                this.buttonBrowseAudio.Enabled = true;
            }
            else
            {
                this.buttonBrowseAudio.Enabled = false;
            }
        }

        private void buttonBrowseAudio_Click(object sender, EventArgs e)
        {
            OpenFileDialog r_Browse = new OpenFileDialog();
            r_Browse.Title = "Select audio file";
            r_Browse.Filter = "MP3 Audio (*.MP3)|*.mp3";
            r_Browse.FilterIndex = 2;
            r_Browse.RestoreDirectory = true;

            if (r_Browse.ShowDialog() == DialogResult.OK)
            {
                AudioPath.Path = r_Browse.FileName;
                buttonBrowseAudio.Text = AudioPath.Path;
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (textBoxTimerTitle.Text.Length == 0)
            {
                textBoxTimerTitle.Text = "<empty>";
            }

            if (GlobalClass.frmTimersStatus == "Add")
            {
                string s = "";

                TimersArray t_TimersArray = new TimersArray();

                t_TimersArray.Name = this.textBoxTimerTitle.Text;
                t_TimersArray.OnTimerEnd = Convert.ToByte(this.comboBoxOnTimerEnd.SelectedIndex);
                t_TimersArray.PlayAudio = this.checkBoxOnTimerEndPlayAudio.Checked;
                t_TimersArray.PlayAudioPath = AudioPath.Path;
                t_TimersArray.PopupMsgBox = this.checkBoxOnTimerEndShowMsgbox.Checked;
                t_TimersArray.MsgBoxString = this.textBoxOnTimerEndMsgbox.Text;
                t_TimersArray.Days = (int)this.numericUpDownDays.Value;
                t_TimersArray.Hours = (int)this.numericUpDownHours.Value;
                t_TimersArray.Minutes = (int)this.numericUpDownMinutes.Value;
                t_TimersArray.Seconds = (int)this.numericUpDownSeconds.Value;
                t_TimersArray.tDays = t_TimersArray.Days;
                t_TimersArray.tHours = t_TimersArray.Hours;
                t_TimersArray.tMinutes = t_TimersArray.Minutes;
                t_TimersArray.tSeconds = t_TimersArray.Seconds;
                t_TimersArray.t_1_over_10_Seconds = 0;

                if (checkBoxShutdownComputer.CheckState == CheckState.Checked)
                {
                    t_TimersArray.ShutdownComputer = 1;
                }

                if (checkBoxRestartComputer.CheckState == CheckState.Checked)
                {
                    t_TimersArray.ShutdownComputer = 2;
                }

                if (checkBoxRun.CheckState == CheckState.Checked)
                {
                    t_TimersArray.openApp = true;
                }
                else
                {
                    t_TimersArray.openApp = false;
                }

                t_TimersArray.appLocation = textBoxRun.Text;

                t_TimersArray.xPossition = Screen.PrimaryScreen.Bounds.Width - 250;
                t_TimersArray.yPossition = 50;
                t_TimersArray.xSize = 200;
                t_TimersArray.ySize = 60;
                t_TimersArray.form_Opacity = 1;
                t_TimersArray.IsFormEnable = false;
                t_TimersArray.form_AlwaysOnTop = false;
                t_TimersArray.timerDeleted = false;
                t_TimersArray.tKey = GlobalClass.timerNextKey;
                t_TimersArray.timerState = 0;

                s = Formatting.Formatting.formatDaysHoursMinutesSecondsLong(t_TimersArray.Days, t_TimersArray.Hours, 
                    t_TimersArray.Minutes, t_TimersArray.Seconds, 0);

                GlobalClass.r_frmMain.lvTimers.Items.Add(GlobalClass.timerNextKey.ToString(), t_TimersArray.Name, null)
                    .SubItems.Add(s);

                t_TimersArray.iIndexOfKey = GlobalClass.r_frmMain.lvTimers.Items.IndexOfKey(
                    GlobalClass.timerNextKey.ToString());

                GlobalClass.r_frmMain.lvTimers.Items[t_TimersArray.iIndexOfKey].Tag = GlobalClass.timerNextKey;
                GlobalClass.r_Timers.Add(GlobalClass.timerNextKey, t_TimersArray);
                GlobalClass.timerNextKey++;
            }

            if (GlobalClass.frmTimersStatus == "Edit")
            {
                int i_Tag = (int)Tag;
                string s = "";

                TimersArray t_TimersArray = GlobalClass.r_Timers[i_Tag];

                t_TimersArray.Name = this.textBoxTimerTitle.Text;
                t_TimersArray.OnTimerEnd = Convert.ToByte(this.comboBoxOnTimerEnd.SelectedIndex);
                t_TimersArray.PlayAudio = this.checkBoxOnTimerEndPlayAudio.Checked;
                t_TimersArray.PlayAudioPath = AudioPath.Path;
                t_TimersArray.PopupMsgBox = this.checkBoxOnTimerEndShowMsgbox.Checked;
                t_TimersArray.MsgBoxString = this.textBoxOnTimerEndMsgbox.Text;
                t_TimersArray.Days = (int)this.numericUpDownDays.Value;
                t_TimersArray.Hours = (int)this.numericUpDownHours.Value;
                t_TimersArray.Minutes = (int)this.numericUpDownMinutes.Value;
                t_TimersArray.Seconds = (int)this.numericUpDownSeconds.Value;
                t_TimersArray.tDays = t_TimersArray.Days;
                t_TimersArray.tHours = t_TimersArray.Hours;
                t_TimersArray.tMinutes = t_TimersArray.Minutes;
                t_TimersArray.tSeconds = t_TimersArray.Seconds;
                t_TimersArray.ShutdownComputer = 0;

                if (checkBoxShutdownComputer.CheckState == CheckState.Checked)
                {
                    t_TimersArray.ShutdownComputer = 1;
                }

                if (checkBoxRestartComputer.CheckState == CheckState.Checked)
                {
                    t_TimersArray.ShutdownComputer = 2;
                }

                if (checkBoxRun.CheckState == CheckState.Checked)
                {
                    t_TimersArray.openApp = true;
                }
                else
                {
                    t_TimersArray.openApp = false;
                }

                t_TimersArray.appLocation = textBoxRun.Text;

                GlobalClass.r_Timers[i_Tag] = t_TimersArray;

                s = Formatting.Formatting.formatDaysHoursMinutesSecondsLong(t_TimersArray.Days, t_TimersArray.Hours,
                    t_TimersArray.Minutes, t_TimersArray.Seconds, 0);

                int indexOfKey = t_TimersArray.iIndexOfKey;

                GlobalClass.r_frmMain.lvTimers.Items[indexOfKey].Text = t_TimersArray.Name;
                GlobalClass.r_frmMain.lvTimers.Items[indexOfKey].SubItems[1].Text = s;

                if (t_TimersArray.IsFormEnable == true)
                {
                    t_TimersArray.TimerForm.labelTimer.Text = 
                        Formatting.Formatting.formatDaysHoursMinutesSecondsShort(t_TimersArray.Days, t_TimersArray.Hours,
                        t_TimersArray.Minutes, t_TimersArray.Seconds, 0);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
        }

        private void comboBoxOnTimerEnd_SelectedIndexChanged(object sender, EventArgs e)
        {
            //System.Windows.Forms.MessageBox.Show(this.comboBoxOnTimerEnd.SelectedIndex.ToString());
        }

        private void numericUpDownDays_ValueChanged(object sender, EventArgs e)
        {
            numericUpDownDays.Select(0, numericUpDownDays.Value.ToString().Length);
        }

        private void numericUpDownHours_ValueChanged(object sender, EventArgs e)
        {
            numericUpDownHours.Select(0, numericUpDownHours.Value.ToString().Length);
        }

        private void numericUpDownMinutes_ValueChanged(object sender, EventArgs e)
        {
            numericUpDownMinutes.Select(0, numericUpDownMinutes.Value.ToString().Length);
        }

        private void numericUpDownSeconds_ValueChanged(object sender, EventArgs e)
        {
            numericUpDownSeconds.Select(0, numericUpDownSeconds.Value.ToString().Length);
        }

        private void numericUpDownDays_GotFocus(object sender, EventArgs e)
        {
            numericUpDownDays.Select(0, numericUpDownDays.Value.ToString().Length);
        }

        private void numericUpDownDays_Click(object sender, EventArgs e)
        {
            numericUpDownDays.Select(0, numericUpDownDays.Value.ToString().Length);
        }

        private void numericUpDownHours_GotFocus(object sender, EventArgs e)
        {
            numericUpDownHours.Select(0, numericUpDownHours.Value.ToString().Length);
        }

        private void numericUpDownHours_Click(object sender, EventArgs e)
        {
            numericUpDownHours.Select(0, numericUpDownHours.Value.ToString().Length);
        }

        private void numericUpDownMinutes_GotFocus(object sender, EventArgs e)
        {
            numericUpDownMinutes.Select(0, numericUpDownMinutes.Value.ToString().Length);
        }

        private void numericUpDownMinutes_Click(object sender, EventArgs e)
        {
            numericUpDownMinutes.Select(0, numericUpDownMinutes.Value.ToString().Length);
        }

        private void numericUpDownSeconds_GotFocus(object sender, EventArgs e)
        {
            numericUpDownSeconds.Select(0, numericUpDownSeconds.Value.ToString().Length);
        }

        private void numericUpDownSeconds_Click(object sender, EventArgs e)
        {
            numericUpDownSeconds.Select(0, numericUpDownSeconds.Value.ToString().Length);
        }

        private void checkBoxShutdownComputer_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxShutdownComputer.CheckState == CheckState.Checked)
            {
                checkBoxRestartComputer.CheckState = CheckState.Unchecked;
            }
        }

        private void checkBoxRestartComputer_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxRestartComputer.CheckState == CheckState.Checked)
            {
                checkBoxShutdownComputer.CheckState = CheckState.Unchecked;
            }
        }

        private void checkBoxRun_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxRun.CheckState == CheckState.Checked)
            {
                textBoxRun.ReadOnly = false;
                buttonBrowseRun.Enabled = true;
                labelOpen.Enabled = true;
            }
            else
            {
                textBoxRun.ReadOnly = true;
                buttonBrowseRun.Enabled = false;
                labelOpen.Enabled = false;
            }
        }

        private void buttonBrowseRun_Click(object sender, EventArgs e)
        {
            OpenFileDialog r_Browse = new OpenFileDialog();
            r_Browse.Title = "Select file";
            r_Browse.Filter = "All Files (*.*)|*.*";
            r_Browse.FilterIndex = 2;
            r_Browse.RestoreDirectory = true;

            if (r_Browse.ShowDialog() == DialogResult.OK)
            {
                appLocation = r_Browse.FileName;
                textBoxRun.Text = appLocation;
            }
        }
    }
}
